package com.example.xyh.serviceImpl;

import com.example.xyh.dao.IOrderListDao;
import com.example.xyh.entity.LineItem;
import com.example.xyh.entity.OrderList;
import com.example.xyh.entity.OrderTable;
import com.example.xyh.service.LineItemService;
import com.example.xyh.service.OrderListService;
import com.example.xyh.service.OrderTableService;
import jakarta.annotation.Resource;
import lombok.extern.apachecommons.CommonsLog;
import org.modelmapper.ModelMapper;
import org.modelmapper.convention.MatchingStrategies;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
@CommonsLog
public class OrderListServiceImpl implements OrderListService {
    @Resource
    IOrderListDao iOrderListDao;
    @Resource
    LineItemService lineItemService;
    @Resource
    OrderTableService orderTableService;
    @Autowired
    protected ModelMapper modelMapper;

    @Override
    public List<com.example.xyh.dto.OrderListDto> findByItemId(Integer item_id) {
        List<OrderList> orderListList = iOrderListDao.findByItemId(item_id);
        List<com.example.xyh.dto.OrderListDto> orderListDTOList = new ArrayList<>();
        for (OrderList orderList : orderListList) {
            com.example.xyh.dto.OrderListDto orderListDTO = com.example.xyh.dto.OrderListDto.builder()
                    .orderId(orderList.getOrderTable().getOrderId())
                    .Id(orderList.getId())
                    .itemId(orderList.getLineItem().getItemId())
                    .itemName(orderList.getLineItem().getItemName())
                    .itemQuantity(orderList.getItemQuantity())
                    .itemPrice(orderList.getLineItem().getItemPrice())
                    .build();
            orderListDTOList.add(orderListDTO);
        }
        return orderListDTOList;
    }

    @Override
    public List<com.example.xyh.dto.OrderListDto> findByOrderId(Integer order_id) {
        List<OrderList> orderListList = iOrderListDao.findByOrderId(order_id);
        List<com.example.xyh.dto.OrderListDto> orderListDTOList = new ArrayList<>();
        for (OrderList orderList : orderListList) {
            com.example.xyh.dto.OrderListDto orderListDTO = com.example.xyh.dto.OrderListDto.builder()
                    .orderId(orderList.getOrderTable().getOrderId())
                    .Id(orderList.getId())
                    .itemId(orderList.getLineItem().getItemId())
                    .itemName(orderList.getLineItem().getItemName())
                    .itemQuantity(orderList.getItemQuantity())
                    .itemPrice(orderList.getLineItem().getItemPrice())
                    .build();
            orderListDTOList.add(orderListDTO);
        }
        return orderListDTOList;
    }

    @Override
    public com.example.xyh.dto.OrderListDto findByOrderIdAndItemId(Integer order_id, Integer item_id) {
        OrderList orderList = iOrderListDao.findByOrderIdAndItemId(order_id, item_id);
        return com.example.xyh.dto.OrderListDto.builder()
                .orderId(orderList.getOrderTable().getOrderId())
                .Id(orderList.getId())
                .itemId(orderList.getLineItem().getItemId())
                .itemName(orderList.getLineItem().getItemName())
                .itemQuantity(orderList.getItemQuantity())
                .itemPrice(orderList.getLineItem().getItemPrice())
                .build();
    }

    @Override
    public com.example.xyh.dto.OrderListDto save(Integer id, Integer item_id, Integer order_id, Integer item_quantity) {
        ModelMapper modelMapper = new ModelMapper();

        modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
        LineItem lineItem = lineItemService.findByItemId(item_id);
        OrderTable orderTable = orderTableService.findByOrderId(order_id);

        if (id == 0) {
            OrderList newOrderList = OrderList.builder()
                    .lineItem(lineItem)
                    .itemQuantity(item_quantity)
                    .orderTable(orderTable)
                    .build();
            OrderList orderList = iOrderListDao.save(newOrderList);
            return com.example.xyh.dto.OrderListDto.builder()
                    .orderId(orderList.getOrderTable().getOrderId())
                    .Id(orderList.getId())
                    .itemId(orderList.getLineItem().getItemId())
                    .itemName(orderList.getLineItem().getItemName())
                    .itemQuantity(orderList.getItemQuantity())
                    .itemPrice(orderList.getLineItem().getItemPrice())
                    .build();
        } else {
            OrderList newOrderList = OrderList.builder()
                    .id(id)
                    .lineItem(lineItem)
                    .itemQuantity(item_quantity)
                    .orderTable(orderTable)
                    .build();
            OrderList orderList = iOrderListDao.save(newOrderList);
            return com.example.xyh.dto.OrderListDto.builder()
                    .orderId(orderList.getOrderTable().getOrderId())
                    .Id(orderList.getId())
                    .itemId(orderList.getLineItem().getItemId())
                    .itemName(orderList.getLineItem().getItemName())
                    .itemQuantity(orderList.getItemQuantity())
                    .itemPrice(orderList.getLineItem().getItemPrice())
                    .build();

        }
    }

    @Override
    public List<com.example.xyh.dto.OrderListDto> findByCustomerId(Integer customer_id) {
        List<OrderList> orderListList = iOrderListDao.findByCustomerId(customer_id);
        List<com.example.xyh.dto.OrderListDto> orderListDTOList = new ArrayList<>();
        for (OrderList orderList : orderListList) {
            com.example.xyh.dto.OrderListDto orderListDTO = com.example.xyh.dto.OrderListDto.builder()
                    .orderId(orderList.getOrderTable().getOrderId())
                    .Id(orderList.getId())
                    .itemId(orderList.getLineItem().getItemId())
                    .itemQuantity(orderList.getItemQuantity())
                    .itemPrice(orderList.getLineItem().getItemPrice())
                    .build();
            orderListDTOList.add(orderListDTO);
        }
        return orderListDTOList;
    }
}
